package com.ideaaedi.plugin.fastcoding.util;

import com.ideaaedi.plugin.fastcoding.constant.FastCodingConstant;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


/**
 * PropertiesComponent util
 *
 * @author JustryDeng
 * @since 2022/1/6 21:10:40
 */
public class PropertiesComponentUtil {
    
    /**
     * 设置application级别的信息变量
     *
     * @param key
     *         键
     * @param value
     *         值
     */
    public static void setValue(@Nullable String key, @Nullable String value) {
        if (key == null) {
            return;
        }
        if (value == null) {
            return;
        }
        //获取 application 级别的 PropertiesComponent
        PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
        propertiesComponent.setValue(FastCodingConstant.PROPERTIES_PREFIX + key, value);
    }
    
    /**
     * 获取application级别的变量值
     *
     * @param key
     *         键
     */
    public static String getValue(@Nullable String key) {
        if (key == null) {
            return "";
        }
        PropertiesComponent propertiesComponent = PropertiesComponent.getInstance();
        String value = propertiesComponent.getValue(FastCodingConstant.PROPERTIES_PREFIX + key);
        return value == null ? "" : value;
    }
    
    /**
     * 设置当前工程级别的信息变量
     *
     * @param project
     *         当前工程
     * @param key
     *         键
     * @param value
     *         值
     */
    public static void setValue(@NotNull Project project, @Nullable String key, @Nullable String value) {
        if (key == null) {
            return;
        }
        if (value == null) {
            return;
        }
        //获取 project 级别的 PropertiesComponent
        PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project);
        propertiesComponent.setValue(FastCodingConstant.PROPERTIES_PREFIX + key, value);
    }
    
    
    /**
     * 获取当前工程级别的信息变量
     *
     * @param project
     *         当前工程
     * @param key
     *         键
     */
    public static String getValue(@NotNull Project project, @Nullable String key) {
        if (key == null) {
            return "";
        }
        PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(project);
        String value = propertiesComponent.getValue(FastCodingConstant.PROPERTIES_PREFIX + key);
        return value == null ? "" : value;
    }
    
}
